from django.shortcuts import render

# Create your views here.
from utils.response import APIResponse
from .models import CourseCategory, Course,CourseChapter
from .serializer import CourseCategorySerializer, CourseSerializer,CourseChapterSerializer
from .pagination import CommonPageNumberPagination
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin,RetrieveModelMixin
from rest_framework.filters import OrderingFilter,SearchFilter
from django_filters.rest_framework import DjangoFilterBackend


class CourseCategoryView(GenericViewSet, ListModelMixin):
    queryset = CourseCategory.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseCategorySerializer


class CourseView(GenericViewSet, ListModelMixin,RetrieveModelMixin):
    queryset = Course.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseSerializer
    # 加入分页---》随着课程越来越多，要分页
    pagination_class = CommonPageNumberPagination
    # 加入排序
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    ordering_fields = ['price', 'students']
    # 加入过滤---》不是按名字搜索的这种过滤，而是按课程分类过滤--》第三方django-filter
    filter_fields = ['course_category']


class CourseChapterView(GenericViewSet, ListModelMixin):
    queryset = CourseChapter.objects.all().filter(is_show=True,is_delete=False).order_by('orders')
    serializer_class =CourseChapterSerializer
    filter_backends = [DjangoFilterBackend]
    # 加入过滤---》按照课程id过滤--》第三方django-filter
    filter_fields = ['course_id']


# 只针对于实战课的搜索接口
class CourseSearchView(GenericViewSet, ListModelMixin):
    queryset = Course.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseSerializer
    filter_backends = [SearchFilter]
    search_fields = ['name',]
    pagination_class = CommonPageNumberPagination


    # 方便后期扩展
    # def list(self, request, *args, **kwargs):
    #     # 这个查的是实战课
    #     res=super().list(request, *args, **kwargs)
    #     # res2=查询免费课
    #     # res3=查询轻课
    #
    #     # 上面全是取数据库查询
    #     # 后期改成取es中查询，
    #     return APIResponse(result={'free_course':'字典','actual_course':res.data})
